package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.controller.RestBaseController;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.model.party.BuildingMeetingEO;
import com.mjh.zhujianju.service.party.BuildingMeetingService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.party.BuildingMeetingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @Description: 会议
 * @date 2018/8/21 21:39
 */
@RestController
@RequestMapping("/meeting")
@Api("会议")
public class MeetingController  extends RestBaseController<BuildingMeetingEO>{

    @Autowired
    private BuildingMeetingService buildingMeetingService;

    @ApiOperation(value="新增会议", notes="新增会议信息")
    @RequestMapping(value = "/addMeeting", method = RequestMethod.POST)
    @ApiImplicitParam(name="会议实体类", value="BuildingMeetingVO", required = true, dataType = "BuildingMeetingVO")
    public ResponseEntity<JsonResult> addMeeting (@RequestBody BuildingMeetingVO buildingMeetingVO){
        JsonResult result = new JsonResult();
        try {
            //Session获取当前登录用户信息
            //根据当前用户所属机构查询该机构下面所有的工作动态
            LoginUserInfo info = XTCurrentUser.getCurrentUser();
            buildingMeetingVO.setCreateUserId(info.getId());
            if(buildingMeetingVO.getStartTime()!=null) {
            	Calendar cal = Calendar.getInstance();
            	cal.setTime(buildingMeetingVO.getStartTime());
            	cal.add(Calendar.MINUTE, buildingMeetingVO.getRemind());
            	buildingMeetingVO.setEndTime(cal.getTime());
            }
            buildingMeetingService.saveInfo(buildingMeetingVO);
            result.setResult("新增成功");
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }
    
   
    @ApiOperation(value="会议列表", notes="查询所有的会议信息 addList：我发起的，joinList：我参与的")
    @RequestMapping(value = "/listMeeting", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getList (){
        JsonResult result = new JsonResult();
        try {
            //Session获取当前登录用户信息
            //根据当前用户所属机构查询该机构下面所有的工作动态
            Long userId = XTCurrentUser.getCurrentUserId();
            Map<String,List<BuildingMeetingVO>> map = buildingMeetingService.queryBuildingMeetingList(userId);
            result.setResult(map);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="会议详情", notes="查询当前会议的详细信息")
    @RequestMapping(value = "/DetailMeeting/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "会议id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getDetail (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
           BuildingMeetingVO vo = buildingMeetingService.queryAPIBuildingMeetingInfo(id);
           if(vo.getStartTime()!=null) {
        	   SimpleDateFormat sdf =new  SimpleDateFormat("yyyy-MM-dd");
        	   SimpleDateFormat sdf2 =new  SimpleDateFormat("HH:mm");
        	   vo.setRemark3(sdf.format(vo.getStartTime()));
        	   vo.setRemark4(sdf2.format(vo.getStartTime()));
        	   
        	   SimpleDateFormat sdf3 =new  SimpleDateFormat("HH:mm");
        	   //处理时间
        	   vo.setTotalTimeStr(sdf3.format(vo.getStartTime())+ "-" +sdf3.format(vo.getEndTime()));
           }
           result.setResult(vo);
           result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

	@Override
	protected BaseService<BuildingMeetingEO> getService() {
		return buildingMeetingService;
	}
}
